package com.talkweb.osmharmony.tileprovider;

import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.media.image.PixelMap;

/**
 * A {@link PixelMapElement} for a {@link com.talkweb.osmharmony.util.MapTileIndex} that has a state to indicate its relevancy:
 * up-to-date (not expired yet), expired, scaled (computed during zoom) and not found (default grey tile)
 */
public class ExpirablePixelMapElement extends PixelMapElement {

    public static final int UP_TO_DATE = -1; // should not be set manually, just leave an empty int[] state
    public static final int EXPIRED = -2;
    public static final int SCALED = -3;
    public static final int NOT_FOUND = -4;
    private static final int defaultStatus = UP_TO_DATE;

    private static final int[] settableStatuses = new int[]{EXPIRED, SCALED, NOT_FOUND};

    private int[] mState;

    public ExpirablePixelMapElement(final PixelMap pPixelMap) {
        super(pPixelMap);
        mState = new int[0];
    }

    public int[] getState() {
        return mState;
    }

    @Override
    public boolean isStateful() {
        return mState.length > 0;
    }

    public boolean setState(final int[] pStateSet) {
        mState = pStateSet;
        return true;
    }

    public static int getState(final Element pTile) {
        if(pTile instanceof ExpirablePixelMapElement){
            for (final int statusItem : ((ExpirablePixelMapElement)pTile).getState()) {
                for (final int statusReference : settableStatuses) {
                    if (statusItem == statusReference) {
                        return statusItem;
                    }
                }
            }
        }
        return defaultStatus;
    }

    public static void setState(final Element pTile, final int status) {
        if(pTile instanceof ExpirablePixelMapElement){
            ((ExpirablePixelMapElement)pTile).setState(new int[]{status});
        }
    }
}
